/**
 * @program: LeetCode
 * @description: LeetCode : 剑指 Offer II 018. 有效的回文
 * @author: WXY
 * @create: 2022-12-23 10:36
 * @Version 1.0
 **/

public class offer_six2_isPalindrome {
    private static boolean isPalindrome(String s) {
        if (s.length() == 0)return true;
        String str = s.replaceAll("[^A-Za-z0-9]+", "");
        char[] strChar = str.toLowerCase().toCharArray();
        int left = 0;
        int right = strChar.length - 1;
        while (left < right) {
            if (strChar[left++] != strChar[right--]) {
                return false;
            }
        }
        return true;
    }

    public static boolean isPalindrome1(String s) {
        int l = 0,r = s.length() - 1;
        char[] arr = s.toCharArray();
        while (l < r) {
            while (l < r && !isValid(arr[l])) l++;
            while (l < r && !isValid(arr[r])) r--;
            if (arr[l] != arr[r] && arr[l] != (arr[r] ^ 32)) {
                return false;
            }
            l++;
            r--;
        }
        return true;
    }

    private static boolean isValid(char x) {
        int val = x - '0';
        if(val <= 9 && val >= 0) return true; // 是数字
        val = x - 'a';
        if(val <= 25 && val >= 0) return true; // 是小写字母
        val = x - 'A';
        if(val <= 25 && val >= 0) return true; // 是大写字母
        return false;
    }

}
